package com.linsen.core.satoken.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.jwt.StpLogicJwtForMixin;
import cn.dev33.satoken.jwt.StpLogicJwtForSimple;
import cn.dev33.satoken.jwt.StpLogicJwtForStateless;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
import com.linsen.core.common.annotation.AutoConfiguration;
import com.linsen.core.common.factory.YamlPropertySourceFactory;
import com.linsen.core.satoken.handler.SaTokenExceptionHandler;
import com.linsen.core.satoken.handler.StpInterfaceHandler;
import com.linsen.core.satoken.props.LinsenSaTokenProperties;
import com.linsen.core.satoken.slf4j.SaLogForSlf4j;
import com.linsen.core.satoken.strategy.JwtStrategy;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * sa-token基础配置类
 *
 * @author zhaoyong
 * @date 2023/3/11
 */
@AutoConfiguration
@EnableConfigurationProperties({LinsenSaTokenProperties.class})
@RequiredArgsConstructor
@ConditionalOnClass({WebMvcConfigurer.class})
@PropertySource(factory = YamlPropertySourceFactory.class, value = "classpath:linsen-satoken.yml")
public class LinsenSaTokenAutoConfiguration implements WebMvcConfigurer {
    private final LinsenSaTokenProperties saTokenProperties;

    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (this.saTokenProperties.getSaInterceptor()) {
            // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
            registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
                    .addPathPatterns(this.saTokenProperties.getSaInterceptorAddPathPatterns())
                    .excludePathPatterns(this.saTokenProperties.getSaInterceptorExcludePathPatterns());
        }
    }

    @Bean
    public SaTokenExceptionHandler saTokenExceptionHandler() {
        return new SaTokenExceptionHandler();
    }

    @Bean
    public StpInterfaceHandler stpInterfaceHandler() {
        return new StpInterfaceHandler();
    }

    @Bean
    @ConditionalOnProperty(prefix = "sa-token", value = "is-log", havingValue = "true")
    public SaLogForSlf4j saLogForSlf4j() {
        return new SaLogForSlf4j();
    }

    @Bean
    @ConditionalOnProperty(prefix = "linsen.sa-token", value = "jwt", havingValue = "true")
    public StpLogic stpLogicJwt() {
        JwtStrategy jwtStrategy = this.saTokenProperties.getJwtStrategy();
        if (JwtStrategy.SIMPLE.equals(jwtStrategy)) {
            return new StpLogicJwtForSimple();
        }

        if (JwtStrategy.MIXIN.equals(jwtStrategy)) {
            return new StpLogicJwtForMixin();
        }

        return new StpLogicJwtForStateless();
    }
}
